## Code for my paper

glerug <- read.table("glerug.asc",header=T)
library(foreign)
trans <- read.dta("transv1.dta")
joint <- merge(glerug,trans[,c("id","pgr","pnbdem","timdep","catho80","muslim80","catpv2","pvatican2")],
    by.x="oid",by.y="id",all.x=T)
library(car)	
con.d <- recode(glerug$polityksg, "c(6,7,8,9,10)=1; else = 0")
con.a <- recode(glerug$polityksg, "c(-6,-7,-8,-9,-10)=1; else = 0")
weak.d <- recode(glerug$polityksg, "c(1,2,3,4,5)=1; else = 0")
weak.a <- recode(glerug$polityksg, "c(-1,-2,-3,-4,-5)=1; else = 0")
hybrid <- recode(glerug$polityksg, "c(1,2,3,4,5,-1,-2,-3,-4,-5)=1; else = 0")

dm.1 <- NULL			# Value of dem change
for(i in 2:nrow(glerug)){
	if(glerug$statenumber[i] == glerug$statenumber[i-1]) {
		dm.1[i] <- glerug$polityksg[i] - glerug$polityksg[i-1]}
	else {dm.1[i] <- 0}
}
dm.1[1] <- 0

dm.2 <- recode(dm.1, "0=0; else = 1")	# Existence of change

dm.3 <- NULL			#Lagged existence of change
for(i in 2:nrow(glerug)){
	dm.3[i] <- dm.2[i-1]
}

dm.4 <- NULL			#3 Year lagged existence of change
for(i in 4:nrow(glerug)){
	if(dm.2[i-1] + dm.2[i-2] + dm.2[i-3] > 0) {dm.4[i] <- 1}
	else {dm.4[i] <- 0}
}

dm.5 <- NULL			# Democratisation
for(i in 2:nrow(glerug)){
	if(dm.1[i] > 0) {dm.5[i] <- 1}
	else {dm.5[i] <- 0}
}

dm.6 <- NULL			# Repression
for(i in 2:nrow(glerug)){
	if(dm.1[i] < 0) {dm.6[i] <- 1}
	else {dm.6[i] <- 0}
}

dm.7 <- NULL			# Lagged Democratisation
for(i in 2:nrow(glerug)){
	if(dm.1[i-1] > 0) {dm.7[i] <- 1}
	else {dm.7[i] <- 0}
}

dm.8 <- NULL			# Lagged Repression
for(i in 2:nrow(glerug)){
	if(dm.1[i-1] < 0) {dm.8[i] <- 1}
	else {dm.8[i] <- 0}
}

glerug2 <- as.data.frame(cbind(joint,con.d,con.a,weak.d,weak.a,hybrid,
			dm.1, dm.2,dm.3, dm.4, dm.5, dm.6, dm.7, dm.8,reform))

glerugto9 <- glerug2[glerug2$polityksg < 10,]

#Analyses

m1 <- glm(onset ~ log(rgdp96pc) 
    + con.d
    + I(POLITY==(-77)) 
    + I(POLITY==(-88))
    + I(PARCOMP==0 | PARCOMP==1) 
    + I(PARREG==1 | PARREG==2)    
    + log(pop) 
    + exp(-py/4)
    + I(irrtrans>0 | itlag>0)
    + I(entry>0) 
    + log((tenure/365)+1)
    + I((entry>0)*log((tenure/365)+1)),
    family=binomial, data=glerug2, na.action=na.omit) 
summary(m1)

m2 <- glm(onset ~ log(rgdp96pc) 
    + con.d
    + dm.7
    + dm.8
    + I(POLITY==(-77)) 
    + I(POLITY==(-88))
    + I(PARCOMP==0 | PARCOMP==1) 
    + I(PARREG==1 | PARREG==2)    
    + log(pop) 
    + exp(-py/4)
    + I(irrtrans>0 | itlag>0)
    + I(entry>0) 
    + log((tenure/365)+1)
    + I((entry>0)*log((tenure/365)+1)),
    family=binomial, data=glerug2, na.action=na.omit) 
summary(m2)

glerug.no <- na.omit(glerug2)
rawscores <- NULL
for(i in 1:21){
rawscores[i] <- sum(glerug.no$onset[glerug.no$polityksg == i-11])/length(glerug.no$polityksg[glerug.no$polityksg == i-11])
}
barplot(rawscores, names.arg = c(-10,-9:10), col = 80, main = "Civil Conflict Incidence by Polity IV Score", xlab = "Polity IV Score", ylab = "Percentage of state years in which conflict onset recorded")


